using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class File : BaseItem
    {
        public enum FileTypes
        {
            None = 0,
            Image = 1
        }

        private File() : base() { }
        private File(Hashtable table) : base(table) { }
        private File(Hashtable table, List<File> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new FileDal();
        }

        public static File CreateInstance() { return new File(); }

        public static File CreateInstance(Acms.Bll.File.FileTypes fileType, System.Web.HttpPostedFile file, byte[] fileData) 
        {
            File tmp = new File();
            tmp.LoadFileData(fileType, file, fileData);
            return tmp;

        }

        public void LoadFileData(Acms.Bll.File.FileTypes fileType, System.Web.HttpPostedFile file, byte[] fileData)
        {
            this.fileData = fileData;
            this.Size = file.ContentLength;
            this.Name = file.FileName;
            this.FileType = fileType;
            this.ContentType = file.ContentType.ToLower();
            switch (this.ContentType)
            {
                case "image/jpeg":
                case "image/pjpeg":
                case "image/jpg":
                    this.extension = ".jpg";
                    break;
                case "image/gif":
                    this.extension = ".gif";
                    break;
                case "image/png":
                    this.extension = ".png";
                    break;
                default:
                    throw new Acms.Exceptions.UnknownFileTypeException(string.Format("Unable to derrive extension from File type '{0}', Please Impliment it", this.ContentType));
            }

            if (fileType == FileTypes.Image)
            {
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(this.GetFileStream());
                this.Width = bitmap.Width;
                this.Height = bitmap.Height;
            }
        }
        
        private static File Get(Hashtable table)
        {
            if (table != null)
            {
                return new File(table);
            }
            else
            {
                return null;
            }
        }

        private static List<File> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<File> tmp = new List<File>();
                foreach (Hashtable table in tables)
	            {
	                tmp.Add(new File(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static File GetById(int id)
        {
            FileDal dal = new FileDal();
            return Get(dal.GetById(id));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((FileDal)dal).Update(ref id, fileData, name, size, fileType, contentType, extension, width, height, ref concurencyId);
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[FileDal.Fields.Id];
            fileData = (byte[])table[FileDal.Fields.FileData];
            name = (string)table[FileDal.Fields.Name];
            size = (long)table[FileDal.Fields.Size];
            fileType = (int)table[FileDal.Fields.FileType];
            contentType = (string)table[FileDal.Fields.ContentType];
            extension = (string)table[FileDal.Fields.Extension];
            width = (int?)table[FileDal.Fields.Width];
            height = (int?)table[FileDal.Fields.Height];
        }

        private int id;
        public int Id
        {
            get { return id;}
        }

        private byte[] fileData;
        public byte[] FileData
        {
            get { return fileData; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private long size;
        public long Size
        {
            get { return size; }
            set { size = value; }
        }
        
        private int fileType;
        public FileTypes FileType
        {
            get { return (FileTypes)fileType; }
            set { fileType = (int)value; }
        }

        private string contentType;
        public string ContentType
        {
            get { return contentType; }
            set { contentType = value; }
        }

        private string extension;
        public string Extension
        {
            get { return extension; }
        }
        
        private int? width;
        public int? Width
        {
            get { return width; }
            set { width = value; }
        }
	

        private int? height;
        public int? Height
        {
            get { return height; }
            set { height = value; }
        }
	
        private System.IO.MemoryStream GetStream(byte[] data)
        {
            return new System.IO.MemoryStream(data);
        }
	
        public System.IO.MemoryStream GetFileStream()
        {
            return GetStream(FileData);
        }


        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((FileDal)dal).Delete(id, concurencyId);
        }
    }
}
